﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Main_Master_SalesPriceGroup : System.Web.UI.Page
{
    Datautility du = new Datautility();
    string SPGID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["SPGID"] != null)
        {
            SPGID = Request.QueryString["SPGID"].ToString();
        }

        if (!IsPostBack)
        {
            BindGrid();
            BtnUpdate.Visible = false;
        }
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
           
            string sql = "select SPGID from ACC_SALESPRICEGROUP where SPGNAME = '" + TxtName.Text+"'" ;
            DataTable DT = du.getdata(sql);
            if (DT.Rows.Count > 0)
            {
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "Price group already exists! ";
            }

            else
            {

                SqlParameter[] param = {
                                 new SqlParameter("@SPGNAME", TxtName.Text.Trim()),
                                 new SqlParameter("@DISCOUNTPER", TxtDiscount.Text.Trim()),
                                 new SqlParameter("@Cname",Session["username"].ToString())
                             };
                du.executeprocedure("usp_insert_ACC_SALESPRICEGROUP", param);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Sales Price Group created successfully...";
                TxtDiscount.Text = "";
                TxtName.Text = "";
                BindGrid();
            }
        }
        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
        }

    }
    protected void Btnclose_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");  
    }

    protected void BindGrid()
    {
        string sql = "select SPGID,SPGNAME, DISCOUNTPER from ACC_SALESPRICEGROUP";
        DataTable dt = du.getdata(sql);
        GrdLocation.DataSource=dt;
        GrdLocation.DataBind();
        custPager.TotalPages = 2; 
    }

    protected void GrdLocation_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdLocation.PageIndex = e.NewPageIndex;
        BindGrid();
    }

    protected void GrdLocation_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        GrdLocation.PageSize = e.CurrentPageSize;
        GrdLocation.PageIndex = e.CurrentPageNumber;
        BindGrid();
    }

    protected void GrdLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        string SPGID = GrdLocation.SelectedDataKey.Value.ToString();
        // Response.Redirect("Location.aspx?WHSID=" + WHSID);
        bindedit(SPGID);
    }

    private void bindedit(string key)
    {
        BtnSave.Visible = false;
        BtnUpdate.Visible = true;
        string sql = "SELECT  SPGID,SPGNAME, DISCOUNTPER from ACC_SALESPRICEGROUP where SPGID =" + key;
        DataTable dt = du.getdata(sql);
        TxtName.Text = dt.Rows[0]["SPGNAME"].ToString();
        HidControl.Value = key;
        TxtDiscount.Text = dt.Rows[0]["DISCOUNTPER"].ToString();
       
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        string SPGID = HidControl.Value;

        string sql = "select SPGID from ACC_SALESPRICEGROUP where SPGNAME = '" + TxtName.Text + "' and SPGID !=" + SPGID;
        DataTable DT = du.getdata(sql);
        if (DT.Rows.Count > 0)
        {
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Price group already exists! ";
        }
        else
        {
            try
            {
                SqlParameter[] param = {
                                       new SqlParameter("@SPGID",SPGID),
                                 new SqlParameter("@SPGNAME", TxtName.Text.Trim()),
                                 new SqlParameter("@DISCOUNTPER", TxtDiscount.Text.Trim()),
                                 new SqlParameter("@ename",Session["username"].ToString())
                             };
                du.executeprocedure("usp_update_ACC_SALESPRICEGROUP", param);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Sales Price Group updated successfully...";
                TxtDiscount.Text = "";
                TxtName.Text = "";
                BindGrid();
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
    }
}
